#+_+ coding:utf-8 +_+

import request
import json
import config


__author__ = 'cz'


class USD2CNY(request.Request):
    def __init__(self):
        super(USD2CNY, self).__init__()
        self.set_host(config.svr_host)
        self.set_path(config.svr_path + "/exchange_rate.do")
        self.set_headers({"Content-type": "application/x-www-form-urlencoded"})


def query():
    req = USD2CNY()
    rsp = req.do_request()
    body = rsp.body()
    body_json = json.loads(body)
    if body_json.has_key("error_code"):
        return None
    rate = body_json["rate"]
    return rate